import { Component, Vue, Watch } from 'vue-property-decorator'
import * as _ from "lodash";
import {namespace} from 'vuex-class'
const SEARCH = namespace('search')
@Component({

})
export default class extends Vue {
  isfocus: boolean = false
  search_val: string = ''
  timer: any = null
  searchList: any[] = []

  @SEARCH.Getter('name') search_key: any

  @Watch('search_val')
  valChange(val:string) {
    // 节流
    if (this.timer) clearTimeout(this.timer)
    this.timer = setTimeout(async () => {
      // 搜索
      console.log(val)
      const {status,data} = await this['$axios'].get(`/search/top`,{
        params: {
          input: val,
          city: this.$store.state.geo.city.replace('市','')
        }
      })
      console.log(status,data);
      if(status === 200) this.searchList = data.length ? data.slice(0,10) : []
      console.log(this.searchList);
    }, 300)
  }

  @Watch('search_key')
  keyChange(news: string) {
    if(news) this.search_val = news
  }
}